package com.xuzz.study.redis.client;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @author xuzz
 * @description
 * @date 2019-12-26 20:01
 */
public class Client {
    private Connection connection;

    public Client(String host, int port) {
        connection = new Connection(host, port);
    }

    public String set(String key, String value) throws UnsupportedEncodingException,IOException {
        if(StringUtils.isEmpty(key))
        {
            throw new IOException("value sent to redis cannot be null");
        }
        set(SafeEncoder.encode(key), SafeEncoder.encode(value));
        return connection.getStatusReply();
    }

    public void set(byte[] key, byte[] value) {
        this.connection.sendCommand(Protocol.Command.SET,new byte[][]{key,value});
    }

    public String get(String key) throws UnsupportedEncodingException,IOException{
        if(StringUtils.isEmpty(key))
        {
            throw new IOException("value sent to redis cannot be null");
        }
        this.connection.sendCommand(Protocol.Command.GET,SafeEncoder.encode(key));
        return connection.getStatusReply();
    }
}
